<?php

/**
 * productCategory admin form
 *
 * @package    trunk
 * @subpackage productCategory
 * @author     Your name here
 */
class ProductCategoryAdminForm extends BaseProductCategoryForm {
    public function configure() {

        parent::configure();

        $this->widgetSchema['parent'] =  new sfWidgetFormDoctrineChoiceNestedSet(array(
                        'model'     => 'ProductCategory',
                        'add_empty' => true,
        ));
        $this->widgetSchema['root_id'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['lft'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['rgt'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['level']= new sfWidgetFormInputHidden();
        // if the current category has a parent, make it the default choice
        if ($this->getObject()->getNode()->hasParent()) {
            $this->setDefault('root_id', $this->getObject()->getNode()->getParent()->getId());
            $this->setDefault('parent', $this->getObject()->getNode()->getParent()->getId());
        }


        // set a validator for parent which prevents a category being moved to one of its own descendants
        $this->validatorSchema['parent'] =  new sfValidatorDoctrineChoiceNestedSet(array(
                        'required' => false,
                        'model'    => 'ProductCategory',
                        'node'     => $this->getObject(),
        ));
        $this->getValidator('parent')->setMessage('node', 'A category cannot be made a descendent of itself.');

    }

    protected function createMediaFormForImageId() {
        // get the DmMedia form
        $form = parent::createMediaFormForImageId();
        $form->validatorSchema['file']->setOption('max_size',(1024*1024*2));
        // choose mime types allowed
        $form->setMimeTypeWhiteList(array(
                'image/jpeg',
                'image/png',
                'image/gif'
        ));

        return $form;
    }
    /**
     * Updates and saves the current object. Overrides the parent method
     * by treating the record as a node in the nested set and updating
     * the tree accordingly.
     *
     * @param Doctrine_Connection $con An optional connection parameter
     */
    public function doSave($con = null) {
        // save the record itself
        parent::doSave($con);
        // if a parent has been specified, add/move this node to be the child of that node
        if ($this->getValue('parent')) {
            $parent = Doctrine::getTable('ProductCategory')->findOneById($this->getValue('parent'));
            if ($this->isNew()) {
                $this->getObject()->getNode()->insertAsLastChildOf($parent);
            }
            else {
                $this->getObject()->getNode()->moveAsLastChildOf($parent);
            }
        }
        // if no parent was selected, add/move this node to be a new root in the tree
        else {
            $categoryTree = Doctrine::getTable('ProductCategory')->getTree();
            if ($this->isNew()) {
                $categoryTree->createRoot($this->getObject());
            }
            else {
                $this->getObject()->getNode()->makeRoot($this->getObject()->getId());
            }
        }
    }
}